
\chapter{Objects}


{\bf T} is an {\it object-oriented\/} language.  {\bf T} programs
are concerned for the most part with creating and manipulating
\iix{objects}, which represent all data, and form the
currency of computation in {\bf T}.  Particular objects are defined not
by bit patterns or by addresses within a computer but rather by their
behavior when called or when passed to procedures which manipulate them.

Objects are obtained most primitively through the use of {\tt QUOTE}
(page \pageref{QUOTE}), {\tt LAMBDA} (page \pageref{LAMBDA}), and {\tt OBJECT}
(page \pageref{OBJECT}) special forms, and less primitively by calling
procedures defined in the standard environment, e.g. {\tt CONS} and
{\tt COMPOSE}, which create new objects or return existing ones.  (An
implementation of {\bf T} would presumably define many such procedures
using more primitive object constructors; for example, {\tt COMPOSE}
might be defined by a {\bf T} program which employs {\tt LAMBDA} to
construct the composed procedure.  Thus the question of what kinds of
objects are truly primitive and which are not is left unanswered by this
manual.)

\section{Literals}


As described in section \ref{section:coreLanguage}, some expressions
evaluate ``to themselves'' or to copies of themselves.  These 
numbers, strings, and characters, and are called
{\it self-evaluating literals.\/}  However, some expressions, lists and
symbols in particular, do not evaluate to themselves.  When an
expression yielding a particular constant value is required,
it is necessary to use {\tt QUOTE}.  Self-evaluating literals
and quoted constants are called \iix{literals}.

Although not all objects have external representations, some objects
which do have external representations have undefined evaluation
semantics.  These  vectors (section \ref{section:vectors}) and the
empty list.  {\tt QUOTE} must also be used to obtain these values as
constants.

\index{QUOTE@{\tt QUOTE}}
{\mbox {\tt (QUOTE {\it object\/}) $\longrightarrow$\  {\it object\/}}}
\hfill Special form

\label{QUOTE}
\begin{inset}{}
Yields {\it object\/}.  The {\it object\/} is not evaluated; thus {\tt QUOTE}
provides a literal notation for constants.
\begin{codexenv}
    \begin{tabular}{lll}
(QUOTE A)         &  $\Longrightarrow$\   & A \\
(QUOTE (A B))      & $\Longrightarrow$\   & (A B) \\
(QUOTE (+ X 8))    & $\Longrightarrow$\   & (+ X 8) \\
(QUOTE (QUOTE A))  & $\Longrightarrow$\   & (QUOTE A)
\end{tabular}
    \end{codexenv}
Since {\tt QUOTE} is used so frequently, an abbreviated external syntax
is provided for {\tt QUOTE} forms: '{\tt {\it object\/}} is an alternative
external representation for the list {\tt (QUOTE {\it object\/})}.
\begin{codexenv}
    \begin{tabular}{lll}
'A          & $\Longrightarrow$\   & A \\
'(A B)      & $\Longrightarrow$\   & (A B) \\
'(QUOTE A)  & $\Longrightarrow$\   & (QUOTE A) \\
''A         & $\Longrightarrow$\   & (QUOTE A)
\end{tabular}
    \end{codexenv}
Objects returned by literal expressions are read-only; they should not
be altered using {\tt SET} or any other side-effecting form.
\end{inset}

\section{Procedures}


A {\it procedure\/}\index{procedures} (or {\it routine\/}\index{routines})
is any object which may be called.  Ordinarily, a procedure is called
as a result of the evaluation of a {\it call\/} (page \pageref{CallSemantics}).
The most primitive mechanism for creating procedures is the
{\tt LAMBDA} special form.
\index{functions}

\index{LAMBDA@{\tt LAMBDA}}
{\mbox {\tt (LAMBDA {\it variables\/} . {\it body\/}) $\longrightarrow$\  {\it procedure\/}}}
\hfill Special form

\label{LAMBDA}
\begin{inset}{}
A {\tt LAMBDA}-expression evaluates to a procedure.
When the procedure is called, the {\it variables\/}
are bound to the arguments to the call, and the {\it body\/}, an implicit
block, is evaluated.  The call yields the value of the last form in the
{\it body\/}.
\begin{codexenv}
((LAMBDA (X Y) (LIST Y X)) 7 'FOO)  $\Longrightarrow$\   (FOO 7)
\end{codexenv}

If {\it variables\/} is not a proper list, but ends in a symbol {\it x\/}, then
the variable {\it x\/} will be bound to a list of the arguments beginning at
the position corresponding to {\it x\/}.

\begin{codexenv}
    \begin{tabular}{lll}
((LAMBDA (X . Y) (LIST Y X)) 7 'FOO 'BAZ)  & $\Longrightarrow$\   & ((FOO BAZ) 7) \\
((LAMBDA Y Y) 7 'FOO 'BAZ)                 & $\Longrightarrow$\   & (7 FOO BAZ)
\end{tabular}
    \end{codexenv}

If {\it body\/} in the {\tt LAMBDA}-expression is null, a syntax error
will be signalled.

If any {\it variable\/} is {\tt ()} instead of a symbol, then the
corresponding argument in a call is ignored.
Also see, {\tt IGNORE}, on page \pageref{IGNORE}.

{\it Scoping:\/}\index{scope}\index{lexical scoping}
The values of the bound variables are apparent only in code lexically
contained in the body of the {\tt LAMBDA}-expression, and not to routines
{\it called\/} from the body.  That is, like SCHEME and ALGOL and unlike
most Lisp dialects, {\bf T} is a lexically scoped language, not a dynamically
scoped language.

{\it Closure:\/}\index{closure}
The {\it procedure\/} is said to be a \iix{closure} of the
{\tt LAMBDA}-expression in the current lexical environment.  That is, when
{\it procedure\/} is called, the {\it body\/} is evaluated in an environment
which is the same as that in which the {\tt LAMBDA}-expression was
evaluated, augmented by the bindings of the {\it variables\/}.  For example,
if {\tt Z} is mentioned in the {\it body\/}, and it is not one of the
{\it variables\/}, then it refers to whatever {\tt Z} was in scope when the
{\tt LAMBDA}-expression was evaluated, {\it not\/} (necessarily)
to the the variable {\tt Z} that is in scope when {\it procedure\/} is called.
\end{inset}

{\tt LAMBDA}-bindings do not shadow syntax table entries in the
standard compiler.  The {\mbox {{\tt standard-compiler}}} and {\sc orbit}, the new
optimizing compiler, now have the same evaluation semantics.
This is consistent with the {\bf T} manual.  Thus,

\begin{codexenv}
(LET ((SET LIST) (X 5)) (SET X 8)) $\Longrightarrow$\  8 {\bf not} (5 8)
\end{codexenv}

However, this doesn't mean that the {\tt LAMBDA}-binding has no effect,
but rather that the binding is not recognized as such when the
name appears in the {\mbox {{\tt CAR}}} of a form.  Thus,

\begin{codexenv}
(LET ((SET LIST) (X 5)) ((BLOCK SET) X 8)) $\Longrightarrow$\  (5 8)
\end{codexenv}

{\bf This is not a final decision.} This was the easiest semantics
to implement, and it is consistent with the documentation.  In
the future lambda bindings may shadow syntax.

\section{Object identity}


Every object has {\it identity\/} in the sense that one may determine
whether two given values are the same object.
\index{equality predicates}
\index{identity}

In the following:
\begin{codexenv}
(DEFINE A (LIST 'P 'Q)) \\
(DEFINE B (LIST 'P 'Q))
\end{codexenv}
there is no way (other than {\tt EQ?}) to distinguish the two objects
which are the values of {\tt A} and {\tt B}.  Nonetheless, since {\tt LIST}
is defined to return a new object each time it is called, the two
objects are distinct; and indeed, a side-effect to one object will not affect
the value of the other:
\begin{codexenv}
(SET (CAR A) 'R) \\
A  $\Longrightarrow$\   (R Q) \\
B  $\Longrightarrow$\   (P Q)
\end{codexenv}

Some system procedures and special forms create new objects, others
return objects which already exist.  In some cases, it is not defined
which of the two happens; it is only guaranteed that some object with
appropriate characteristics is returned.  For example, {\tt CONS} creates
new objects; {\tt QUOTE} expressions yield pre-existing constant objects;
and numerical routines such as {\tt +} may or may not create new numbers.

The {\tt EQ?} predicate primitively determines object identity.

\index{EQ?@{\tt EQ?}}
{\mbox {\tt (EQ? {\it object1 object2\/}) $\longrightarrow$\  {\it boolean\/}}}
\hfill 

\label{EQ?}
\begin{inset}{}
Returns true if {\it object1\/} and {\it object2\/} are identically the same
object.  {\tt EQ?} is the {\it finest\/} comparison predicate for
objects, in the sense that if {\tt EQ?} cannot distinguish two objects,
then neither can any other equality predicate.
\end{inset}

\index{NEQ?@{\tt NEQ?}}
{\mbox {\tt (NEQ? {\it object1 object2\/}) $\longrightarrow$\  {\it boolean\/}}}
\hfill 

\begin{inset}{}
{\tt NEQ?} is the logical complement of {\tt EQ?}.
\begin{codexenv}
(NEQ? {\it object1 object2\/}) $\equiv$ (NOT (EQ? {\it object1 object2\/}))
\end{codexenv}
\end{inset}

Uniqueness of literals (other than symbols and characters) isn't defined
in general.  For example,
\begin{codexenv}
(EQ? '(A B C) '(A B C))
\end{codexenv}
may yield either true or false, depending on the implementation.
Two similar-looking literal expressions in different places
may or may not yield distinct objects.

However, a given literal expression will always yield the same object
each time it is evaluated. \begin{codexenv}
(LET ((F (LAMBDA () '(A B C)))) (EQ? (F) (F)))  $\Longrightarrow$\   {\it true}
\end{codexenv}

\section{Symbols}

\label{section:symbols}
{\it Symbols\/}\index{symbols} are named objects which have wide applicability as
tokens and identifiers of various sorts.  Symbols are uniquely
determined by their names, and have a convenient external syntax
by which they may be obtained.

Uniqueness of symbols {\it is\/} defined:
\begin{codexenv}
(EQ? 'FOO 'FOO)  $\Longrightarrow$\   {\it true}
\end{codexenv}


\index{SYMBOL?@{\tt SYMBOL?}}
{\mbox {\tt (SYMBOL? {\it object\/}) $\longrightarrow$\  {\it boolean\/}}}
\hfill Type predicate

\label{SYMBOL?}
\begin{inset}{}
Returns true if {\it object\/} is a symbol.
\end{inset}

See also {\tt SYMBOL->STRING} and {\tt STRING->SYMBOL},
page \pageref{string-to-symbol}.

\section{Predicates and truth values}


Conditional expressions in {\bf T} (see section
\ref{section:conditionals}) usually involve the evaluation of a
\iix{test} expression; the object yielded by the test is then used to
make a control decision, depending on whether the value is \iix{false}
or \iix{true}.  There is one distinguished false value called \#{\tt F}.
Any other value is considered to be true.  A value
intended to be used in this way is called a \iix{boolean} or \iix{truth value}.
\index{conditionals}

A \iix{predicate} is any procedure which yields truth values.
Predicates are typically given names which end in {\tt ?},
e.g. {\tt NULL?} and {\tt EQ?}.  Calls to predicates are
naturally used as test expressions.

An \iix{equality predicate} is a two-argument predicate which is
side-effectless and unaffected by side-effects, and acts like an
equivalence relation in the mathematical sense.

The canonical boolean objects have convenient read syntax associated
with them.

\index{\#{\tt T}@{\tt \#{\tt T}}}
{\mbox {\tt \#{\tt T}  {\it reads as}  {\it true}}}
\hfill Read syntax

\begin{inset}{}
Read syntax for canonical {\it true} object.
\end{inset}

\index{\#{\tt F}@{\tt \#{\tt F}}}
{\mbox {\tt \#{\tt F}  {\it reads as}  {\it false}}}
\hfill Read syntax

\begin{inset}{}
Read syntax for canonical {\it false} object.
\begin{codexenv}
    \begin{tabular}{lll}
(true? '\#{\tt T}) &$\Longrightarrow$\ & {\it true} \\
(false? (car '(\#{\tt F} \#{\tt T}))) &$\Longrightarrow$\ & {\it true} \\
\end{tabular}
    \end{codexenv}
\end{inset}

\index{()@{\tt ()}}
{\mbox {\tt ()  {\it reads as}  {\it empty-list}}}
\hfill Read syntax

\begin{inset}{}
Read syntax for {\it empty-list} object.
\end{inset}

\index{NIL@{\tt NIL}}
{\mbox {\tt NIL $\longrightarrow$\  \#{\tt F}}}
\hfill 

\begin{inset}{}
This system variable has as its value the canonical false object \#{\tt F}.
\end{inset}

\index{T@{\tt T}}
{\mbox {\tt T $\longrightarrow$\  \#{\tt T}}}
\hfill 

\begin{inset}{}
The system variable {\tt T} has as its value the canonical true value.
Later versions of {\bf T} may leave the system variable {\tt T} unbound,
so users should not depend on these semantics.
Any non-false value is considered to be true for the purposes of tests.
\end{inset}

(), \#{\tt T}, and \#{\tt F} are elements of read syntax, not evaluatable symbols.
\begin{codexenv}
    \begin{tabular}{lll}
(car '(\#{\tt T} foo)) & $\Longrightarrow$\  & \#{\tt T} \\
(list \#{\tt T} foo) & $\Longrightarrow$\  & {\it undefined}, {\rm and is probably a syntax error.} \\
{\rm In {\bf T3.1}:} () & $\Longrightarrow$\  & () {\rm with} {\it warning}. \\
{\rm In future:} () & $\Longrightarrow$\  & {\it error} \\
\end{tabular}
    \end{codexenv}

In {\bf T3}, the canonical false value '\#{\tt F} is not necessarily the
same object as the empty list, ().
For compatibility with previous versions, in {\bf T3.1} {\it false}
and the empty list will continue to be the same object, but this
will change in a future release.
\begin{codexenv}
(eq? '\#{\tt F} '()) $\longrightarrow$\  {\it undefined} \\
\end{codexenv}
In {\bf T3.1} {\mbox {{\tt (eq? \#{\tt F} ()} $\Longrightarrow$\  {\it true}')}}.

It is an error to use () in an evaluated position.  This error
currently generates a warning and treats () as '(), i.e. as
if the empty list were self evaluating.  An error will be
signalled in the future.  Use '() for empty
lists, and {\tt NULL?} to check for the empty list.
{\tt NIL} or '\#{\tt F} can be used for false values, and {\tt NOT}
to check for false values.

\section{Types}


A \iix{type} may be seen either as a collection of objects
with similar behavior, or as a characteristic function for such a
collection, or as the behavior characteristic of similar objects; these
views are equivalent.

A \iix{type predicate} is a predicate which is defined on all objects and
whose value is not affected by any side-effects (that is, calling the
type predicate on a particular object will return the same value
regardless of the point at which it is called with respect to arbitrary
other computations).  Type predicates are usually used to determine a
given object's membership in a type.

\section{Continuations}

\label{section:continuations}
\index{CALL-WITH-CURRENT-CONTINUATION@{\tt CALL-WITH-CURRENT-CONTINUATION}}
{\mbox {\tt (CALL-WITH-CURRENT-CONTINUATION {\it proc}) $\longrightarrow$\  {\it value-of-proc}}}
\hfill proc

\begin{inset}{}
The procedure {\mbox {\tt CALL-WITH-CURRENT-CONTINUATION}} packages
up the current continuation as an ``escape procedure'' and passes
it as an argument to {\it proc}.  {\it proc} must be a procedure of one
argument.  The escape procedure is an n-ary procedure, which
if later invoked with zero or more arguments, will ignore whatever
continuation is in effect at that later time and will instead pass
the arguments to whatever continuation was in effect
at the time the escape procedure was created.

The escape procedure created by
{\mbox {\tt CALL-WITH-CURRENT-CONTINUATION}} has unlimited extent
just like any other procedure.  It may be stored in variables
or data structures and may be called as many times as desired.
For a more thorough explanation consult the Revised3 Report on Scheme.
\end{inset}
